﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Locations
{
    /// <summary>
    /// Interaction logic for ContactPoints.xaml
    /// </summary>
    public partial class ContactPoints : UserControl
    {
        private SalesManagerEntities _context = null;

        public ContactPoints()
        {
            InitializeComponent();
            DataContextChanged += new DependencyPropertyChangedEventHandler(ContactPoints_DataContextChanged);
        }

        void ContactPoints_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (DataContext != null)
            {
                foreach (tblContactPoint contactPoint in ((tblPerson)DataContext).ContactPoints)
                    contactPoint.ContactPointTypeReference.Load();
            }
        }

        public void SetContext(SalesManagerEntities context)
        {
            _context = context;
        }

        private void AddContactPoint_Click(object sender, RoutedEventArgs e)
        {
            if (_context != null && this.DataContext != null)
            {
                dialogContactPointType dlg = new dialogContactPointType(_context);
                if ((bool)dlg.ShowDialog())
                {
                    tblPerson person = (tblPerson)this.DataContext;

                    tblContactPoint newContactPoint = new tblContactPoint();
                    newContactPoint.Person = person;
                    newContactPoint.ContactPointType = dlg.SelectedContactPointType;

                    gridContactPoints.SelectedItem = newContactPoint;
                    gridContactPoints.Focus();
                }
            }
        }

        private void RemoveContactPoint_Click(object sender, RoutedEventArgs e)
        {
            _context.DeleteObject((tblContactPoint)gridContactPoints.SelectedItem);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
        }

    }
}
